#include <stdio.h>
#include <string.h>

#include "pseudocode.h"
#include "tokens.h"
#include "keywords.h"
#include "parser.h"

#define MANTBITSFLOAT 22
#define MAXASMSIZE 0x1000000
#define MAXPARMS 4

char rodataasm[MAXASMSIZE] = "",bssasm[MAXASMSIZE] = "",textasm[MAXASMSIZE] = "",stmtasm[MAXASMSIZE] = ""
,exprasm[MAXASMSIZE] = "",parmasm[MAXASMSIZE] = "",parmauxasm[MAXPARMS][MAXASMSIZE],funcasm[MAXASMSIZE] = ""
,genasm[MAXASMSIZE] = "";
char auxasm[100];
int regFPU = -1;

void concatgenasm(){
  if(strcmp(rodataasm,"") != 0) strcat(genasm,rodataasm);
  if(strcmp(bssasm,"") != 0) strcat(genasm,bssasm);
  strcat(genasm,textasm);
}

void concattextasm(char *source){
  strcat(textasm,source);
  strcpy(source,"");
}

void concatfuncasm(char *source){
  strcat(funcasm,source);
  strcpy(source,"");
}

void concatstmtasm(char *source){
  strcat(stmtasm,source);
  strcpy(source,"");
}

void concatexprasm(char *source){
  strcat(exprasm,source);
  strcpy(source,"");
}

void initparmauxasm(){
  strcpy(parmauxasm[3],"");
  strcpy(parmauxasm[2],"");
  strcpy(parmauxasm[1],"");
  strcpy(parmauxasm[0],"");
}

void concatparmauxasm(char *source,int index){
  strcat(parmauxasm[index],source);
  strcpy(source,"");
}

void concatparmasm(){
  if(strcmp(parmauxasm[3],"") != 0) strcat(parmasm,parmauxasm[3]);
  if(strcmp(parmauxasm[2],"") != 0) strcat(parmasm,parmauxasm[2]);
  if(strcmp(parmauxasm[1],"") != 0) strcat(parmasm,parmauxasm[1]);
  strcat(parmasm,parmauxasm[0]);
}

char *convertfloat2ieee(char *lexval,char *floatlexval){
	int sign,exp,mantissa,floatieee;
	float f = strtof(lexval,NULL);

	//cálculo do sinal
	sign = (f < 0);
	if(sign) f *= -1;

	//cálculo do expoente
	exp = 0;

	if(f > 1){ 	
		while(f >= 2){
			f /= 2;
			exp++;
		}
	}else if (f < 1 && f > 0){
		while(f < 1){
			f *= 2;
			exp--;
		}
	}

	if(f != 0) exp += 127;

	//cálculo da mantissa (entender o número como uma cadeia de bits)
	f-= 1;
	float div = 2.0f;
	int aprox = 0;
	mantissa = 0;
	while(aprox < 22){ //bits de aproximação (de 0 a 22)
		if((f - 1.0f/div) >= 0){
			f -= 1.0f/div;
			mantissa++; //bit atual = 1
		}
		div *= 2.0f; // div vai de 2¹ a 2²³
		aprox++;
		mantissa <<= 1; //passa para o próximo bit
	}
	if(f > 0) mantissa++; //corrige erro de aproximação (feito após testes)

	//junção do padrão
	floatieee = (sign << 31) + (exp << 23) + mantissa;

	//conversão para string
	sprintf(floatlexval,"0x%8.8X",floatieee);

	return floatlexval;
}

void funcgen(char* funcName){
  /** */
  sprintf(auxasm, "\n\t.globl %s\n",funcName);
  strcat(funcasm,auxasm);
  //fprintf(target, "\n\t.globl %s\n",funcName);
  sprintf(auxasm, "\t.type %s,@function\n\n",funcName);
  strcat(funcasm,auxasm);
  //fprintf(target, "\t.type %s,@function\n\n",funcName);
  sprintf(auxasm, "%s: \n",funcName);
  strcat(funcasm,auxasm);
  //fprintf(target, "%s: \n",funcName);
  sprintf(auxasm, "\tpushl %%ebp\n");
  strcat(funcasm,auxasm);
  //fprintf(target, "\tpushl %%ebp\n");
  sprintf(auxasm, "\tmovl (%%esp),%%ebp\n");
  strcat(funcasm,auxasm);
  //fprintf(target, "\tmovl (%%esp),%%ebp\n");
  /** */
}

void endfuncgen(int otype,char* funcName){
  if(otype == FUNCTYPE){ 
    sprintf(auxasm, "%s.conclusion:\n",funcName);
    strcat(funcasm,auxasm);
    //fprintf(target,"%s.conclusion:\n",funcName);
  }
  sprintf(auxasm, "\tmovl %%ebp,(%%esp)\n");
  strcat(funcasm,auxasm);
  //fprintf(target, "\tmovl %%ebp,(%%esp)\n");
  sprintf(auxasm, "\tpopl %%ebp\n");
  strcat(funcasm,auxasm);
  //fprintf(target, "\tpopl %%ebp\n");
  sprintf(auxasm, "\tret\n",funcName);
  strcat(funcasm,auxasm);
  //fprintf(target, "\tret\n");
}

void exitgen(){
  sprintf(auxasm, "\n#end program (exit)\n");
  strcat(textasm,auxasm);
  //fprintf(target, "\n#end program (exit)\n");
  sprintf(auxasm, "\tmovl $1,%%eax\n");
  strcat(textasm,auxasm);
  //fprintf(target, "\tmovl $1,%%eax\n");
  sprintf(auxasm, "\tmovl $0,%%ebx\n");
  strcat(textasm,auxasm);
  //fprintf(target, "\tmovl $0,%%ebx\n");
  sprintf(auxasm, "\tint $0x80\n");
  strcat(textasm,auxasm);
  //fprintf(target, "\tint $0x80\n");
}

void rodatagen(){
  sprintf(auxasm, "\n\t.section .rodata\n");
  strcat(rodataasm,auxasm);
  //fprintf(target, "\n\t.section .rodata\n");
}

void textgen(){
  sprintf(auxasm, "\n\t.section .text\n");
  strcat(textasm,auxasm);
  //fprintf(target, "\n\t.section .text\n");
}

void bssgen(int sect){
  if(sect == PROGRAM){
    sprintf(auxasm, "\n\t.section .bss\n");
    strcat(bssasm,auxasm);
    //fprintf(target, "\n\t.section .bss\n");
  }
}

void declvargen(int sect,char *var,int size){
  if(sect == PROGRAM){
    sprintf(auxasm, "\t .comm %s_,%d,%d\n",var,size,size);
    strcat(bssasm,auxasm);
    //fprintf(target, "\t .comm %s_,%d,%d\n",var,size,size);
  }
}

void lblflowgen(int lbl){
  sprintf(auxasm, ".L%d:\n", lbl);
  strcat(stmtasm,auxasm);
  //fprintf(target, ".L%d:\n", lbl);
}

void lblgen(int lbl){
  sprintf(auxasm, ".L%d:\n", lbl);
  strcat(stmtasm,auxasm);
  //fprintf(target, "%s:\n", lbl);
}

void jmptruegen(int lbl,int aux){
  switch(aux){
    case '=':
      sprintf(auxasm, "\tje .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tje .L%d\n", lbl);
      break;
    case '<':
      sprintf(auxasm, "\tjl .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjl\t.L%d\n", lbl);
      break;
    case '>':
      sprintf(auxasm, "\tjg .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjg\t.L%d\n", lbl);
      break;
    case GEQ:
      sprintf(auxasm, "\tjge .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjge\t.L%d\n", lbl);
      break;
    case LEQ:
      sprintf(auxasm, "\tjle .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjle\t.L%d\n", lbl);
      break;
    case NEQ:
      sprintf(auxasm, "\tjne .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjne .L%d\n", lbl);
      break;
  }
}

void jmpfalsegen(int lbl,int aux){
  switch(aux){
    case '=':
      sprintf(auxasm, "\tjne .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjne .L%d\n", lbl);
      break;
    case '<':
      sprintf(auxasm, "\tjge .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjge\t.L%d\n", lbl);
      break;
    case '>':
      sprintf(auxasm, "\tjle .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjle\t.L%d\n", lbl);
      break;
    case GEQ:
      sprintf(auxasm, "\tjl .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjl\t.L%d\n", lbl);
      break;
    case LEQ:
      sprintf(auxasm, "\tjg .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tjg\t.L%d\n", lbl);
      break;
    case NEQ:
      sprintf(auxasm, "\tje .L%d\n", lbl);
      strcat(stmtasm,auxasm);
      //fprintf(target, "\tje .L%d\n", lbl);
      break;
  }
}

void jmpgen(int lbl){
  sprintf(auxasm, "\tjmp .L%d\n", lbl);
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tjmp .L%d\n", lbl);
}

void jmplblgen(int lbl){
  sprintf(auxasm, "\tjmp .L%d\n", lbl);
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tjmp %s\n", lblName);
}
void jmplblconcgen(char * lblName){
  sprintf(auxasm, "\tjmp %s.conclusion\n", lblName);
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tjmp %s.conclusion\n", lblName);
}

void relopgen()
{
  sprintf(auxasm, "\tcmpl\t%%eax,(%%esp)\n");
  strcat(exprasm,auxasm);
  //fprintf(target, "\tcmpl\t%%eax,(%%esp)\n");
}

void addopgen(int aux,int acctype)
{  
  switch(aux){
    case OR:
      if(acctype == INTCONV || acctype == BOOLCONV){
	sprintf(auxasm, "\torl\t%%eax,(%%esp)\n");
	strcat(exprasm,auxasm);
	//fprintf(target, "\torl\t%%eax,(%%esp)\n");
      }
      break;
    case '+':
      if(acctype == INTCONV || acctype == BOOLCONV){
	sprintf(auxasm, "\taddl\t%%eax,(%%esp)\n");
	strcat(exprasm,auxasm);
	//fprintf(target, "\taddl\t%%eax,(%%esp)\n");
      }else if(acctype == FLOATCONV){
	sprintf(auxasm, "\tfaddps\t(%%eax),%%st(%d)\n",regFPU);
	regFPU--;
	strcat(exprasm,auxasm);
	//fprintf(target, "\tfadds\t%%eax,(%%esp)\n");
      }
      break;
    case '-':
      if(acctype == INTCONV || acctype == BOOLCONV){
	sprintf(auxasm, "\tsubl\t%%eax,(%%esp)\n");
	strcat(exprasm,auxasm);
	//fprintf(target, "\tsubl\t%%eax,(%%esp)\n");
      }else if(acctype == FLOATCONV){
	sprintf(auxasm, "\tfsubps\t(%%eax),%%st(%d)\n",regFPU);
	regFPU--;
	strcat(exprasm,auxasm);
	//fprintf(target, "\tfsubs\t%%eax,(%%esp)\n");
      }
      break;
  }
  
  sprintf(auxasm, "\tpopl\t%%eax\n");
  strcat(exprasm,auxasm);
  //fprintf(target, "\tpopl\t%%eax\n");
}

void mulopgen(int aux,int acctype)
{  
  switch(aux){
    case AND:
      if(acctype == INTCONV || acctype == BOOLCONV){
	sprintf(auxasm, "\tandl\t%%eax,(%%esp)\n");
	strcat(exprasm,auxasm);
	//fprintf(target, "\tandl\t%%eax,(%%esp)\n");
      }
      break;
    case '*':
      if(acctype == INTCONV || acctype == BOOLCONV){
	sprintf(auxasm, "\tpopl %%ebx\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tcltd\n");
	strcat(exprasm,auxasm);
	//fprintf(target, "\tcltd\n");
	sprintf(auxasm, "\timull\t%%ebx\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tpushl %%eax\n");
	strcat(exprasm,auxasm);
	//fprintf(target, "\timull\t(%%esp)\n");
      }else if(acctype == FLOATCONV){
	sprintf(auxasm, "\tfmulps\t(%%esp)\n");
	regFPU--;
	strcat(exprasm,auxasm);
	//fprintf(target, "\tfmuls\t(%%esp)\n");
      }
      break;
    case '/':
      if(acctype == INTCONV || acctype == BOOLCONV){
	sprintf(auxasm, "\tpopl %%ebx\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tcltd\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tidivl\t%%ebx\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tpushl %%eax\n");
	strcat(exprasm,auxasm);
      }else if(acctype == FLOATCONV){
	sprintf(auxasm, "\tfdivps\t(%%esp)\n");
	regFPU--;
	strcat(exprasm,auxasm);
	//fprintf(target, "\tfdivs\t(%%esp)\n");
      }
      break;
    case DIV:
	sprintf(auxasm, "\tpopl %%ebx\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tcltd\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tidivl\t%%ebx\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tpushl %%eax\n");
	strcat(exprasm,auxasm);
      break;
    case MOD:
	sprintf(auxasm, "\tpopl %%ebx\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tcltd\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tidivl\t%%ebx\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tpushl %%eax\n");
	strcat(exprasm,auxasm);
	sprintf(auxasm, "\tmovl %%edx,%%eax\n");
	strcat(exprasm,auxasm);
      break;
  }
  
  sprintf(auxasm, "\tpopl\t%%eax\n");
  strcat(exprasm,auxasm);
  //fprintf(target, "\tpopl\t%%eax\n");
}

void notgen(){
  if(acctype == INTCONV || acctype == BOOLCONV){
    sprintf(auxasm, "\tnotl %%eax\n");
    strcat(exprasm,auxasm);
    //fprintf(target,"\tnotl %%eax\n");
  }else if(acctype == FLOATCONV){
    sprintf(auxasm, "\tfnos (%%eax)\n");
    strcat(exprasm,auxasm);
    //fprintf(target,"\tfnots %%eax\n");
  }
}

void neggen(){
  if(acctype == INTCONV || acctype == BOOLCONV){
    sprintf(auxasm, "\tnegl %%eax\n");
    strcat(exprasm,auxasm);
    //fprintf(target,"\tnegl %%eax\n");
  }else if(acctype == FLOATCONV){
    sprintf(auxasm, "movl 0xBF800000,%%ebx\n");
    sprintf(auxasm, "\tflds (%%eax)\n");
    sprintf(auxasm, "fmuls (%%ebx),%%st(0)\n");
    sprintf(auxasm, "\tfstps (%%eax)\n");
    strcat(exprasm,auxasm);
    //fprintf(target,"\tfnegs %%eax\n");
  }
}

void moveeaxtoecxgen(){
  sprintf(auxasm, "\tmovl %%eax,%%ecx\n");
  strcat(exprasm,auxasm);
}

void incecxgen(int offset,char *varName){
  if(offset == 0){
    sprintf(auxasm, "\tincl %s_\n",varName);
    strcat(stmtasm,auxasm);
  }else{
    sprintf(auxasm, "\tincl %d(ebp)\n",offset);
    strcat(stmtasm,auxasm);
  }
}

void dececxgen(int offset,char *varName){
  if(offset == 0){
    sprintf(auxasm, "\tdecl %s_\n",varName);
    strcat(stmtasm,auxasm);
  }else{
    sprintf(auxasm, "\tdecl %d(ebp)\n",offset);
    strcat(stmtasm,auxasm);
  }
}

void cmpforgen(int offset,char *varName){
  if(offset == 0){
    sprintf(auxasm, "\tcmpl\t%%ecx,%s_\n",varName);
    strcat(exprasm,auxasm);
  }else{
    sprintf(auxasm, "\tcmpl\t%%ecx,%d(ebp)\n",offset);
    strcat(exprasm,auxasm);
  }
}

void pushgen(int type,int index){
  sprintf(auxasm, "\tpushl %%eax\n");
  switch(type){
    case EXPR: strcat(exprasm,auxasm); break;
    case STMT: strcat(stmtasm,auxasm); break;
    case PARM: strcat(parmauxasm[index],auxasm); break;
  }
  //fprintf(target,"\tpushl %%eax\n");
}

void popgen(int idtype){
  sprintf(auxasm, "\tpopl %%eax\n");
  switch(idtype){
    case EXPR: strcat(exprasm,auxasm); break;
    case STMT: strcat(stmtasm,auxasm); break;
  }
  //fprintf(target,"\tpopl %%eax\n");
}

void callgen(char *funcName,int idtype){
  sprintf(auxasm,"\tcall %s\n",funcName);
  switch(idtype){
    case EXPR: strcat(exprasm,auxasm); break;
    case STMT: strcat(stmtasm,auxasm); break;
  }
  //fprintf(target,"\tcall %s\n",funcName);
}

void assigngen(int offset,char *varName){
  if(offset==0){
    if(acctype == FLOATCONV){
      sprintf(auxasm, "\tfstps (%%eax)\n");
      regFPU--;
      strcat(stmtasm,auxasm);
      //fprintf(target,"\tfstps %%eax\n");
    }
    sprintf(auxasm, "\tmovl %%eax,%s_\n",varName);
    strcat(stmtasm,auxasm);
    //fprintf(target,"\tmovl %%eax,%s_\n",varName);
  }else{
    if(acctype == FLOATCONV){
      sprintf(auxasm, "\tfstps (%%eax)\n");
      regFPU--;
      strcat(exprasm,auxasm);
      //fprintf(target,"\tfstps %%eax\n");
    }
    sprintf(auxasm, "\tmovl %%eax,%d(%%ebp)\n",offset);
    strcat(exprasm,auxasm);
    //fprintf(target,"\tmovl %%eax,%d(%%ebp)\n",offset);
  }
}

void assignuintgen(int acctype,char *val,char *floatval){
  if(acctype == INTCONV || acctype == BOOLCONV){
    if(strcmp(val,"0") <= 0){
      sprintf(auxasm, "\tmovl $0,%%eax\n");
      strcat(exprasm,auxasm);
      //fprintf(target,"\tmovl $0,%%eax\n");
    }else{
      sprintf(auxasm, "\tmovl $%s,%%eax\n",val);
      strcat(exprasm,auxasm);
      //fprintf(target,"\tmovl $%s,%%eax\n",val);
    }
  }else if(acctype == FLOATCONV){
    if(strcmp(val,"0") <= 0){
      sprintf(auxasm, "\tfmovz\n");
      regFPU++;
      strcat(exprasm,auxasm);
      //fprintf(target,"\tmovl $0,%%eax\n");
    }else{
      sprintf(auxasm, "\tmovl $%s,%%eax\n",convertfloat2ieee(val,floatval));
      strcat(exprasm,auxasm);
      //fprintf(target,"\tmovl $%s,%%eax\n",convertfloat2ieee(val,floatval));
      sprintf(auxasm, "\tflds (%%eax)\n");
      regFPU++;
      strcat(exprasm,auxasm);
      //fprintf(target,"\tflds %%eax\n");
    }
  }
}

void assignfloatgen(int acctype,char *val,char *floatval){
  sprintf(auxasm,"\tmovl $%s,%%eax\n",convertfloat2ieee(val,floatval));
  strcat(stmtasm,auxasm);
  //fprintf(target,"\tmovl $%s,%%eax\n",convertfloat2ieee(val,floatval));
  sprintf(auxasm,"\tflds (%%eax)\n");
  regFPU++;
  strcat(stmtasm,auxasm);
  //fprintf(target,"\tflds %%eax\n");
}

void varexprgen(int offset,char *varName){
  if(offset==0){
    sprintf(auxasm,"\tmovl %s_,%%eax\n",varName);
    strcat(exprasm,auxasm);
    //fprintf(target,"\tmovl %s_,%%eax\n",varName);
    if(acctype == FLOATCONV){
      sprintf(auxasm, "\tflds %%eax\n");
      regFPU++;
      strcat(exprasm,auxasm);
      //fprintf(target,"\tflds %%eax\n");
    }
  }else{
    sprintf(auxasm,"\tmovl %d(%%ebp),%%eax\n",offset);
    strcat(exprasm,auxasm);
    //fprintf(target,"\tmovl %d(%%ebp),%%eax\n",offset);
    if(acctype == FLOATCONV){
      sprintf(auxasm, "\tflds (%%eax)\n");
      regFPU++;
      strcat(exprasm,auxasm);
      //fprintf(target,"\tflds %%eax\n");
    }
  }
}

void promotiongen(int acctype){
  if(acctype == FLOATCONV){
    sprintf(auxasm, "\tflds (%%eax)\n");
    regFPU++;
    strcat(exprasm,auxasm);
    //fprintf(target,"\tflds %%eax\n");
  }
}
  
void strconstgen(int lbl,char *str){
  if(strcmp(rodataasm,"") == 0) rodatagen();
  sprintf(auxasm, "\tLC%d: .asciz \"%s\"\n",lbl,str);
  strcat(rodataasm,auxasm);
  sprintf(auxasm, "\tLI%d = .-LC%d\n",lbl,lbl);
  strcat(rodataasm,auxasm);
  //fprintf(target, "\t.asciz \"%s\"\n",str);
}

void strconstlngen(int lbl,char *str){
  if(strcmp(rodataasm,"") == 0) rodatagen();
  sprintf(auxasm, "\tLC%d: .asciz \"%s\\n\"\n",lbl,str);
  strcat(rodataasm,auxasm);
  sprintf(auxasm, "\tLI%d = .-LC%d\n",lbl,lbl);
  strcat(rodataasm,auxasm);
  //fprintf(target, "\t.asciz \"%s\\n\"\n",str);
}

void printgen(int lbl){
  sprintf(auxasm, "\tpushl %%eax\n");
  strcat(stmtasm,auxasm);
  sprintf(auxasm, "\tpushl %%ebx\n");
  strcat(stmtasm,auxasm);
  sprintf(auxasm, "\tpushl %%ecx\n");
  strcat(stmtasm,auxasm);
  sprintf(auxasm, "\tpushl %%edx\n");
  strcat(stmtasm,auxasm);
  sprintf(auxasm, "\tmovl $4,%%eax\n");
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tmovl $4,%%eax\n");
  sprintf(auxasm, "\tmovl $1,%%ebx\n");
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tmovl $1,%%ebx\n");
  sprintf(auxasm, "\tmovl\t$LC%d, %%ecx\n",lbl);
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tmovl\t$.LC%d, %%ecx\n",0);
  sprintf(auxasm, "\tmovl\t$LI%d, %%edx\n",lbl);
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tmovl\t$.LI%d, %%edx\n",0);
  sprintf(auxasm, "\tint $0x80\n");
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tint $0x80\n");
  sprintf(auxasm, "\tpopl %%edx\n");
  strcat(stmtasm,auxasm);
  sprintf(auxasm, "\tpopl %%ecx\n");
  strcat(stmtasm,auxasm);
  sprintf(auxasm, "\tpopl %%ebx\n");
  strcat(stmtasm,auxasm);  
  sprintf(auxasm, "\tpopl %%eax\n");
  strcat(stmtasm,auxasm);
}

void readgen(){
  sprintf(auxasm, "\tmovl $3,%%eax\n");
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tmovl $4,%%eax\n");
  sprintf(auxasm, "\tmovl $1,%%ebx\n");
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tmovl $1,%%ebx\n");
  sprintf(auxasm, "\tmovl\t$.LC%d, %%ecx\n",0);
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tmovl\t$.LC%d, %%ecx\n",0);
  sprintf(auxasm, "\tmovl\t$.LI%d, %%edx\n",0);
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tmovl\t$.LI%d, %%edx\n",0);
  sprintf(auxasm, "\tint $0x80\n");
  strcat(stmtasm,auxasm);
  //fprintf(target, "\tint $0x80\n");
}
